<?php



/**
 * 判断是否存在这个用户
 * @param $id
 * @return bool
 */
function hasUser($id){
    $users = fetchData('users');
    foreach($users as $value){
        if($value['id'] == $id)
            return true;
    }
    return false;
}

/**
 * 判断存在
 * @param $id
 * @return bool
 */
function hasBook($id){
    $books = fetchData('books');
    foreach($books as $value){
        if($value['id'] == $id)
            return true;
    }
    return false;
}


/**
 * 判断有效
 * @param $id
 * @return bool
 */
function effectBook($id){
    if(!hasBook($id)) return false;
    $books = fetchData('books');
    $book = null;
    foreach($books as $value){
        if($value['id'] == $id) {
            $book = $value;
            break;
        }
    }
    return $book['status']?true:false;
}

/**
 * 添加外借记录
 * @param $user
 * @param $book
 */
function appendRecord($user, $book){
    $data = "\n".$user.'|'.$book.'|'.time().'|0';
    file_put_contents('./data/record.dat', $data, FILE_APPEND);
}

/**
 * 显示帮助信息
 */
function displayHelp(){
    echo "\n";
    echo "   欢迎使用xxxx图书管理系统   \n";
    echo "/----------------------------/\n";
    echo "/请使用以下命令执行相关操作：/\n";
    echo "/ --card 创建一张新的卡      /\n";
    echo "/ --borrow 借书              /\n";
    echo "/ --return 还书              /\n";
    echo "/ --addbook 添加书籍         /\n";
    echo "/----------------------------/\n";
}

/**
 * 办卡操作
 */
function makeCard(){

	//创建PDO对象
	$dsn = 'mysql:dbname=shcool;host=localhost';
	$user = 'root';
	$password = '123456';
	$db = new PDO($dsn,$user,$password);
	
    do {

        // 获取用户输入信息
        echo "/----------------------------/\n";
        echo "请输入用户姓名：";
        $username = trim(fgets(STDIN));
        echo "/----------------------------/\n";
        echo "请输入用户学号：";
        $number = trim(fgets(STDIN));

		while($db->query('SELECT * FROM student WHERE id='.$number)->fetch()){
			echo "学号已存在，请重新输入学号：";
            $number = trim(fgets(STDIN));
		}

        // 用户开卡信息核实
        echo "您需要办理的开卡信息：\n";
        echo "用户名:" . $username . "\n";
        echo "学号:" . $number . "\n";
        echo "输入Y确认,任意键重新输入:";

        $confirm = trim(fgets(STDIN));
        $confirm = strtolower($confirm);

    } while($confirm != 'y');

	$result = $db->exec('INSERT INTO student (id, name) values('.$number.',"'.$username.'")');

	if($result) {
        echo "开卡成功\n";
        echo "/----------------------------/\n";
    }
	else {
        echo "开卡失败\n";
        echo "/----------------------------/\n";
    }
}

/**
 * 借书操作
 */
function makeBorrow()
{
    //创建PDO对象
    $dsn = 'mysql:dbname=shcool;host=localhost';
    $user = 'root';
    $password = '123456';
    $db = new PDO($dsn, $user, $password);

    // 获取用户卡号信息
    echo "请输入借书卡号：";
    $id = trim(fgets(STDIN));

    // 不存在重新输入
    while (!$db->query('SELECT * FROM student WHERE id=' . $id)->fetch()) {
        echo "不存在卡号，请重新输入：";
        $id = trim(fgets(STDIN));
    }

    echo "请输入图书编号：";
    $book = trim(fgets(STDIN));

    //判断图书是否存在
    while (!$db->query('SELECT * FROM book WHERE  library = 1 and id=' . $book)->fetch()) {
        echo "该书不存在或者已外借，请重新输入";
        $book = trim(fgets(STDIN));
    }


    appendRecord($id, $book);

    $time = time();
    $time += (24 * 60 * 60 * 7);
    echo "借阅成功！请在" . date('Y-m-d', $time) . "之前归还！\n";
    $db->exec('UPDATE book SET library = null WHERE id=' . $book);

}

/**
 * 还书操作
 */

function makeReturn(){

    //创建PDO对象
    $dsn = 'mysql:dbname=shcool;host=localhost';
    $user = 'root';
    $password = '123456';
    $db = new PDO($dsn, $user, $password);

    // 获取用户卡号信息
    echo "请输入卡号：";
    $id = trim(fgets(STDIN));
    while (!$db->query('SELECT * FROM student WHERE id=' . $id)->fetch()) {
        echo "不存在卡号，请重新输入：";
        $id = trim(fgets(STDIN));
    }

    //获取图书编号
    echo "请输入图书编号：";
    $book = trim(fgets(STDIN));
    while (!$db->query('SELECT * FROM book WHERE  id=' . $book)->fetch()) {
        echo "输入有误，请重新输入";
        $book = trim(fgets(STDIN));
    }
    echo "还书成功啦！";
    $db->exec('UPDATE book SET library = 1 WHERE id=' . $book);

}

/**
 * 添加书籍操作
 */
function addbook(){

    //创建PDO对象
    $dsn = 'mysql:dbname=shcool;host=localhost';
    $user = 'root';
    $password = '123456';
    $db = new PDO($dsn, $user, $password);

    //获取图书编号
    echo "请输入图书编号：";
    $book = trim(fgets(STDIN));
    while ($db->query('SELECT * FROM book WHERE  id=' . $book)->fetch()) {
        echo "该书编号已存在，请重新输入图书编号：";
        $book = trim(fgets(STDIN));
    }
    //获取书名
    echo "请输入书名：";
    $name = trim(fgets(STDIN));
    $db->exec('INSERT INTO book SET id = '.$book.',name="'.$name.'"');
    echo "添加成功啦！";
}














